package com.shine.icis.action;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.UUID;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.esim.struts.webutil.SessionParam;
import com.shine.icis.exception.ICISException;
import com.shine.icis.util.EFRameInf;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.SeStock;
import com.shine.icis.dto.SeStockHis;
import com.shine.icis.dto.HsStock;
import com.shine.icis.dto.HsStockHis;
import com.shine.icis.blo.IStockQueryService;
import com.shine.icis.blo.IStockCheckService;

/**
 * 报送数据调整
 * @author zhangxr
 *
 */
public class StockQueryAction extends BaseAction {

	private static final long serialVersionUID = 7638121893867812633L;
	
	private IStockQueryService stockQueryService;
	private IStockCheckService stockCheckService;
	//input param
	private int pageNo;
	private int pageSize;
	
	private String stockAccount;
	private String stockCode;
	private String fundAccount;
	private String queryDate;
	private String expType;//导出方式
	
	//output param
	private String defaultDate;//当前交易日期
	
	private ListPage<SeStock> seStockPage;
	private ListPage<SeStockHis> seStockHisPage;
	private ListPage<HsStock> hsStockPage;
	private ListPage<HsStockHis> hsStockHisPage;
	private String downloadUrl;//下载地址
	
	
	public void setStockQueryService(IStockQueryService stockQueryService) {
		this.stockQueryService = stockQueryService;
	}
	public void setStockCheckService(IStockCheckService stockCheckService) {
		this.stockCheckService = stockCheckService;
	}
	
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public void setStockAccount(String stockAccount){
		this.stockAccount = stockAccount;
	}
	
	public void setStockCode(String stockCode){
		this.stockCode = stockCode;
	}
	
	public void setFundAccount(String fundAccount){
		this.fundAccount = fundAccount;
	}
	
	public void setQueryDate(String queryDate){
		this.queryDate = queryDate;
	}
	
	public void setExpType(String expType) {
		this.expType = expType;
	}
	public String getDefaultDate() {
		return defaultDate;
	}
	
	
	public ListPage<SeStock> getSeStockPage() {
		return seStockPage;
	}
	public ListPage<SeStockHis> getSeStockHisPage() {
		return seStockHisPage;
	}
	public ListPage<HsStock> getHsStockPage() {
		return hsStockPage;
	}
	public ListPage<HsStockHis> getHsStockHisPage() {
		return hsStockHisPage;
	}
	public String getDownloadUrl() {
		return downloadUrl;
	}
	/**
	 * 转到查询页面
	 * @return
	 */
	public String initQuerySeStock() {
		//currentBusiDate = stockQueryService.getBusiDate();
		
		return success();
	}
	
	/**
	 * 查询
	 * @return
	 */
	public String querySeStock() {
		QueryParameters qp = new QueryParameters();
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		queryCommSet(qp);
		try {
			seStockPage = stockQueryService.getSeStockPage(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	
	/**
	 * 导出Excel
	 */
	public String exportSeStock() {
		QueryParameters qp = new QueryParameters();
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			queryCommSet(qp);
			String temFileName = stockQueryService.csExpDataSeStock(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
	/**
	 * 转到查询页面
	 * @return
	 */
	public String initQuerySeStockHis() {
		String currentDate;
		String previousBusiDate;
		
		currentDate = stockCheckService.getCurrentDate();
		defaultDate = stockCheckService.getPreviousBusiDate(currentDate);
		previousBusiDate = stockCheckService.getPreviousBusiDate(defaultDate);
		
		return success();
	}
	/**
	 * 查询
	 * @return
	 */
	public String querySeStockHis() {
		QueryParameters qp = new QueryParameters();
		int queryDate1;
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		queryCommSet(qp);
		if (queryDate != null && !queryDate.trim().equals("")) {
			queryDate1 = Integer.parseInt(queryDate.trim().replaceAll("-", ""));
			qp.addParameter("queryDate", queryDate1);
		}
		
		try {
			seStockHisPage = stockQueryService.getSeStockHisPage(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	
	/**
	 * 导出Excel
	 */
	public String exportSeStockHis() {
		QueryParameters qp = new QueryParameters();
		int queryDate1;
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			queryCommSet(qp);
			if (queryDate != null && !queryDate.trim().equals("")) {
				queryDate1 = Integer.parseInt(queryDate.trim().replaceAll("-", ""));
				qp.addParameter("queryDate", queryDate1);
			}
			
			String temFileName = stockQueryService.csExpDataSeStockHis(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
	/**
	 * 转到查询页面
	 * @return
	 */
	public String initQueryHsStock() {
		//currentBusiDate = stockQueryService.getBusiDate();
		
		return success();
	}
	/**
	 * 查询
	 * @return
	 */
	public String queryHsStock() {
		QueryParameters qp = new QueryParameters();
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		queryCommSet(qp);
		try {
			hsStockPage = stockQueryService.getHsStockPage(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	/**
	 * 导出Excel
	 */
	public String exportHsStock() {
		QueryParameters qp = new QueryParameters();
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			queryCommSet(qp);
			String temFileName = stockQueryService.csExpDataHsStock(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
	/**
	 * 转到查询页面
	 * @return
	 */
	public String initQueryHsStockHis() {
		String currentDate;
		String previousBusiDate;
		
		currentDate = stockCheckService.getCurrentDate();
		defaultDate = stockCheckService.getPreviousBusiDate(currentDate);
		previousBusiDate = stockCheckService.getPreviousBusiDate(defaultDate);
		
		return success();
	}
	/**
	 * 查询
	 * @return
	 */
	public String queryHsStockHis() {
		QueryParameters qp = new QueryParameters();
		int queryDate1;
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		queryCommSet(qp);
		if (queryDate != null && !queryDate.trim().equals("")) {
			queryDate1 = Integer.parseInt(queryDate.trim().replaceAll("-", ""));
			qp.addParameter("queryDate", queryDate1);
		}
		
		try {
			hsStockHisPage = stockQueryService.getHsStockHisPage(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	
	/**
	 * 导出Excel
	 */
	public String exportHsStockHis() {
		QueryParameters qp = new QueryParameters();
		int queryDate1;
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			queryCommSet(qp);
			if (queryDate != null && !queryDate.trim().equals("")) {
				queryDate1 = Integer.parseInt(queryDate.trim().replaceAll("-", ""));
				qp.addParameter("queryDate", queryDate1);
			}
			
			String temFileName = stockQueryService.csExpDataHsStockHis(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
	private void queryCommSet(QueryParameters qp){
		if (stockAccount != null && !stockAccount.trim().equals("")) {
			qp.addParameter("stockAccount", stockAccount);
		}
		if (stockCode != null && !stockCode.trim().equals("")) {
			qp.addParameter("stockCode", stockCode);
		}
		if (fundAccount != null && !fundAccount.trim().equals("")) {
			qp.addParameter("fundAccount", Integer.parseInt(fundAccount.trim()));
		}
	}
}
